﻿#region Header

/*
Remote views based on linq
By

Pascal Hauser 
Dipl. Ing. in Informatik, Hochschule für Technik Rapperswil, 2006
Master Thesis, Hochschule für Technik Rapperswil, 2008-2010

and

Raphael Gfeller
Dipl. Ing. in Informatik, Hochschule für Technik Rapperswil, 2006
Master Thesis, Hochschule für Technik Rapperswil, 2008-2010

*/

#endregion

#region Usings

using System.Collections.Generic;
using Sebarf.Core.Interfaces;
using Sebarf.Core.Internal;
using Sebarf.Diagnostics.Interfaces;
using Sebarf.ProcessChains.Interfaces;
using Sebarf.Services.Impl;
using Sebarf.Services.Interfaces;
using Utils;

#endregion

namespace Sebarf.ProcessChains {
	/// <summary>
	/// A handler for a ResolveProcessChainDependenciesRequest.
	/// We load the configuration file and build the process chains.
	/// </summary>
	public class ResolveProcessChainDependenciesRequestHandler : RequestHandler<ResolveProcessChainDependenciesRequest> {
		#region Public Methods

		protected override object OnProcessRequest(ResolveProcessChainDependenciesRequest request,
													IServiceLocatorService serviceLocator) {
			Check.AssertException(serviceLocator.IsServiceAvailable<IDiagnosticsServiceLocatorService>(),
								   "IDiagnosticsServiceLocatorService has to be available");

			var service = serviceLocator.GetRegisteredService<IProcessChainLocatorService>();
			var localEngine = serviceLocator.GetRegisteredService<IRequestHandlerEngineLocatorService>();
			IList<IProcessStep> steps = service.GetProcessChains();
			foreach (IProcessStep step in steps) {
				IProcessStep toProcess = step;
				while (toProcess != null) {
					localEngine.GetLocalEngine().Process(new ResolveDependenciesRequest { ResolveMyReferences = toProcess });
					toProcess = toProcess.GetNextStep();
				}
			}
			return true;
		}

		#endregion
	}
}